<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 获取前端传过来的用户 ID
$user_id = $_POST['user_id'] ?? '';

// 检查用户 ID 是否为空
if (empty($user_id)) {
    $result = [
        'status' => 'error',
        'message' => '用户 ID 不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 查询用户上传的所有视频信息
    $selectVideosStmt = $pdo->prepare("SELECT * FROM mi_videos WHERE 上传者用户id = :user_id");
    $selectVideosStmt->bindParam(':user_id', $user_id);
    $selectVideosStmt->execute();
    $videos = $selectVideosStmt->fetchAll(PDO::FETCH_ASSOC);

    if (!empty($videos)) {
        // 返回用户上传的所有视频信息
        $result = [
            'status' => 'success',
            'message' => '获取视频信息成功',
            'videos' => $videos
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    } else {
        // 用户没有上传视频
        $result = [
            'status' => 'error',
            'message' => '该用户没有上传任何视频'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    }

} catch (PDOException $e) {
    // 数据库操作失败
    $result = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
?>